package com.hotel.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.hotel.entity.ServiceEntity;
import com.hotel.dto.ServiceItemDTO;
import com.hotel.dto.ServicePageDto;
import com.hotel.result.PageResult;
import com.hotel.result.Result;
import com.hotel.service.ServiceService;
import com.hotel.vo.ServiceItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author zxq
 */
@RestController
@RequestMapping("/service")
@Api(tags = "服务项目接口")
public class ServiceController {

    @Autowired
    private ServiceService serviceService;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final String SERVICE_ = "service_";
    private static final String SERVICE_KEY = "service:list";


    @ApiOperation("查询所有服务项目")
    @GetMapping("/list")
    public Result<PageResult> list(@ApiParam("分页参数") ServicePageDto servicePageDto) {

        PageResult pageResult = serviceService.list(servicePageDto);

        return Result.success(pageResult);
    }

    @ApiOperation("添加服务项目")
    @PostMapping
    public Result add(@ApiParam("添加参数") @RequestBody ServiceItemDTO service) {
        serviceService.add(service);
        return Result.success();
    }

    @ApiOperation("删除服务项目")
    @DeleteMapping("/{ids}")
    public Result delete(@ApiParam("删除参数") @PathVariable Integer[] ids) {
        Integer i = serviceService.removedByIds(ids);
        return Result.success(i);
    }

    @ApiOperation("修改服务项目")
    @PutMapping
    public Result update(@ApiParam("修改参数") @RequestBody ServiceEntity service) {
        serviceService.updateById(service);
        return Result.success();
    }

    @ApiOperation("查询单个项目")
    @GetMapping("/{id}")
    public Result<ServiceItemVO> getOne(@ApiParam("查询参数") @PathVariable Integer id) {
        Object object = redisTemplate.opsForHash().get(SERVICE_KEY, SERVICE_ + id);
        if (ObjectUtils.isNotEmpty(object)) {
            if (object instanceof ServiceItemVO){
                ServiceEntity serviceEntity = (ServiceEntity) object;
                ServiceItemVO serviceItemVO = new ServiceItemVO();
                BeanUtils.copyProperties(serviceEntity, serviceItemVO);
                return Result.success(serviceItemVO);
            }
        }
        ServiceEntity service = serviceService.getById(id);
        redisTemplate.opsForHash().put(SERVICE_KEY, SERVICE_ + id, service);
        ServiceItemVO serviceItemVO = new ServiceItemVO();
        BeanUtils.copyProperties(service, serviceItemVO);
        return Result.success(serviceItemVO);
    }

    @ApiOperation("获取列表")
    @GetMapping("/all")
    public Result<List<ServiceEntity>> list() {
        return Result.success(serviceService.getall());
    }


}
